function [newPosHOGS newNegHOGS newPosRects newNegRects] = tryOnNewImage(im, W, posRECTexamples, xMean);
    
SVMthresh = 0.1;
clf;
figure(1);
imagesc(im);
hold on;
rects = applyLinearSVMToImage(im, W, SVMthresh, posRECTexamples);

% Visualize rectangles from applyLinear
% above line took a long time, the bored user might have clicked
% on some other window...
figure(1); 
for ix = 1:size(rects,1);
    rectangle('Position',rects(ix,:));
end
hold off;
title('rects after applyLinearSVM ');


% ok, so lets do local refinement:

% given an image and a rectangle, and a classifier, find the local max of
% the classifier, despite changes in the rectangle:
% (a) top left corner location
% (b) bottom right corner location.

disp('local optimizations of rectangles');
options = optimset('TolX', .5,'MaxFunEvals', 100);
for rx = 1:size(rects,1);
    rstart = rects(rx,:);
    % next line is a crappy hack to get fminsearch to have good size initla
    % step size...
    rnew = fminsearch('computeSVMrect',rstart, options, im, W, xMean,rstart);
    rects(rx,:) = rnew;
end

%% Now display rectangles and get updates.
figure(1);
clf;
imagesc(im);
hold all;
for rx = 1:size(rects,1);
    rectangle('Position', rects(rx,:));
    text(rects(rx,1)+rects(rx,3)+2, rects(rx,2)+rects(rx,4)+2, num2str(rx),...
         'fontSize', 16,'fontWeight', 'bold', ...
    'BackgroundColor',[.7 .9 .7]);
end
title('list all rectangles that correctly surround a person');
disp('all others will be used as new false positive examples');
rects = round(rects);
figure(3);
clf;
for ix = 1:size(rects,1)
    subplot(3,7,ix);
    imagesc(im(rects(ix,2):rects(ix,2)+rects(ix,4),...
        rects(ix,1):rects(ix,1)+rects(ix,3),:));
    title(ix);
    axis off;
end
%%
confirmedPeople = ...
    input('List rectangle numbers with good people, in form [1 2 4 5], (or just) return for none ');

newPosRects = [];
if length(confirmedPeople)>0
    newPosRects = rects(confirmedPeople,:);  
end
newPosHOGS = [];
for ix = 1:length(confirmedPeople)
   [hog rectData] = makeHOGandRECT(im,newPosRects(ix,:));
   newPosHOGS(:,ix) = hog(:);
end

newNegRects = rects;
newNegRects(confirmedPeople,:) = [];
newNegHOGS = [];
for ix = 1:size(newNegRects,1);
   [hog rectData] = makeHOGandRECT(im,newNegRects(ix,:));
   newNegHOGS(:,ix) = hog(:);   
end

%% now ask user to draw any missing people

% first, redraw the positive rectangles...
figure(1); clf;
imagesc(im);
hold all;
for rx = 1:size(newPosRects,1);
    rectangle('Position', newPosRects(rx,:));
end

% now ask uesr to add new rectangles...
title('type (r) to outline a missed person, or (n) to be done');
userInput = 'r';
numPosRects = length(confirmedPeople);

while userInput == 'r';
    userInput = input('type (r) to outline a missed person, or (n) to be done ','s');
    switch userInput
        case 'r',
            disp('r');
            numPosRects = numPosRects + 1;
            r = getRectFromUser(im);
            newPosRects(end+1,:) = r;
            
            [hog rectData] = makeHOGandRECT(im,r);
            newPosHOGS(:,numPosRects) = hog(:);
        case 'n',
            disp('n');
        otherwise
            
            disp(['unknown input: ' userInput]);
            userInput = 'r';  % keep going...
    end
end


